//
// RS Game Framework
// Copyright © 2009 Jedd Haberstro
// jhaberstro@gmail.com
// 
// $Id:
//

#if RS_PLATFORM == RS_PLATFORM_MACOSX

#include "rs/application/cocoa/CocoaGraphicsContext.hpp"
#include "rs/application/cocoa/CocoaWindow.hpp"

namespace rs
{
    namespace application
    {
        RS_DEFINE_REFLECTION_SUBCLASS(rs::application::CocoaGraphicsContext, rs::graphics::GraphicsContext);
		
		CocoaGraphicsContext::CocoaGraphicsContext(Window* window) {
			RS_ASSERT(window != 0, "window is null");
            RS_ASSERT(window->GetClassRecord().IsSubclass(rs::application::CocoaWindow::ClassRecord), "Invalid window");
            rs::application::CocoaWindow* cocoaWindow = static_cast< rs::application::CocoaWindow* >(window);
            openglContext_ = [[NSOpenGLContext alloc] initWithFormat:cocoaWindow->GetOpenGLPixelFormat() shareContext:nil];
            [openglContext_ setView: cocoaWindow->GetCocoaView()];
            [openglContext_ makeCurrentContext];
		}

        CocoaGraphicsContext::~CocoaGraphicsContext() {
            [openglContext_ release];
        }
        
        void CocoaGraphicsContext::Update() {
            [openglContext_ update];
        }
        
        Boolean CocoaGraphicsContext::Begin() {
            [openglContext_ makeCurrentContext];
            return true;
        }
        
        Boolean CocoaGraphicsContext::End() {
            [openglContext_ flushBuffer];
            return true;
        }
    }
}

#endif // RS_PLATFORM == RS_PLATFORM_MACOSX
